package com.dgut.service.impl;

import com.dgut.dao.HousingDao;
import com.dgut.dao.UserDao;
import com.dgut.model.Housing;
import com.dgut.model.PageBean;
import com.dgut.model.User;
import com.dgut.model.UserInfo;
import com.dgut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author GooRay
 * 创建于 2020/6/21
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private HousingDao housingDao;

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public List<UserInfo> findAllMessage() {
        List<UserInfo> userInfos = new ArrayList<>();
        List<User> users = userDao.findAll();
        Housing housing;
        for (User user : users) {
            housing= housingDao.findByUserID(user.getUser_id());
            UserInfo userInfo = new UserInfo();
            userInfo.setUser_id(user.getUser_id());
            userInfo.setUser_name(user.getUser_name());
            userInfo.setUser_id_card(user.getUser_id_card());
            userInfo.setBuilding(housing.getBuilding());
            userInfo.setRoom_number(housing.getRoom_number());
            userInfo.setLastest_time(user.getLastest_time());
            userInfo.setLastest_temp(user.getLastest_temp());
            userInfos.add(userInfo);
        }
        return userInfos;
    }

    @Override
    public List<UserInfo> findAllMessageByPage(List<User> users) {
        List<UserInfo> userInfos = new ArrayList<>();
        Housing housing;
        for (User user : users) {
            housing= housingDao.findByUserID(user.getUser_id());
            UserInfo userInfo = new UserInfo();
            userInfo.setUser_id(user.getUser_id());
            userInfo.setUser_name(user.getUser_name());
            userInfo.setUser_id_card(user.getUser_id_card());
            userInfo.setBuilding(housing.getBuilding());
            userInfo.setRoom_number(housing.getRoom_number());
            userInfo.setLastest_time(user.getLastest_time());
            userInfo.setLastest_temp(user.getLastest_temp());
            userInfos.add(userInfo);
        }
        return userInfos;
    }

    @Override
    public List<UserInfo> findByTemp() {
        List<User> users = userDao.findByTemp(37);
        return findAllMessageByPage(users);
    }

    @Override
    public PageBean<UserInfo> findUserByPage(String current_Page, String _rows, Map<String, String[]> condition) {
        //1.创建一个空的PageBean对象
        PageBean<UserInfo> pageBean = new PageBean<UserInfo>();
        //2.设置参数
        int currentPage =  Integer.parseInt(current_Page);
        int rows = Integer.parseInt(_rows);
        //获取总条数
        int totalCount = userDao.findTotalCount(condition);
        //获取开始
        int start = (currentPage-1)*rows;
        List<User> list = userDao.findByPage(start,rows,condition);
        List<UserInfo> userInfos = findAllMessageByPage(list);
        //计算总页面
        int totalPage = totalCount % rows == 0 ? totalCount / rows : totalCount / rows + 1;
        if(currentPage<=1){
            currentPage=1;
        }
        if(currentPage>=totalPage){
            currentPage=totalPage;
        }
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        pageBean.setList(userInfos);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }


}
